//
//  ZLFGetAllScreenSize.m
//  PersonalOCTest
//
//  Created by zlf on 2025/9/15.
//

#import "ZLFGetAllScreenSize.h"
#import <UIKit/UIKit.h>
#import <sys/utsname.h>

@implementation ZLFGetAllScreenSize
+ (void)listAllKnownLogicalResolutions {
    NSLog(@"=== iPhone 逻辑分辨率列表 ===");
    
    // iPhone 历代机型逻辑分辨率
    NSArray *iPhoneResolutions = @[
        @{@"model": @"iPhone", @"resolution": @"320x480", @"scale": @"1x"},
        @{@"model": @"iPhone 3G", @"resolution": @"320x480", @"scale": @"1x"},
        @{@"model": @"iPhone 3GS", @"resolution": @"320x480", @"scale": @"1x"},
        @{@"model": @"iPhone 4", @"resolution": @"320x480", @"scale": @"2x"},
        @{@"model": @"iPhone 4S", @"resolution": @"320x480", @"scale": @"2x"},
        @{@"model": @"iPhone 5", @"resolution": @"320x568", @"scale": @"2x"},
        @{@"model": @"iPhone 5c", @"resolution": @"320x568", @"scale": @"2x"},
        @{@"model": @"iPhone 5s", @"resolution": @"320x568", @"scale": @"2x"},
        @{@"model": @"iPhone 6", @"resolution": @"375x667", @"scale": @"2x"},
        @{@"model": @"iPhone 6 Plus", @"resolution": @"414x736", @"scale": @"3x"},
        @{@"model": @"iPhone 6s", @"resolution": @"375x667", @"scale": @"2x"},
        @{@"model": @"iPhone 6s Plus", @"resolution": @"414x736", @"scale": @"3x"},
        @{@"model": @"iPhone SE (1st)", @"resolution": @"320x568", @"scale": @"2x"},
        @{@"model": @"iPhone 7", @"resolution": @"375x667", @"scale": @"2x"},
        @{@"model": @"iPhone 7 Plus", @"resolution": @"414x736", @"scale": @"3x"},
        @{@"model": @"iPhone 8", @"resolution": @"375x667", @"scale": @"2x"},
        @{@"model": @"iPhone 8 Plus", @"resolution": @"414x736", @"scale": @"3x"},
        @{@"model": @"iPhone X", @"resolution": @"375x812", @"scale": @"3x"},
        @{@"model": @"iPhone XR", @"resolution": @"414x896", @"scale": @"2x"},
        @{@"model": @"iPhone XS", @"resolution": @"375x812", @"scale": @"3x"},
        @{@"model": @"iPhone XS Max", @"resolution": @"414x896", @"scale": @"3x"},
        @{@"model": @"iPhone 11", @"resolution": @"414x896", @"scale": @"2x"},
        @{@"model": @"iPhone 11 Pro", @"resolution": @"375x812", @"scale": @"3x"},
        @{@"model": @"iPhone 11 Pro Max", @"resolution": @"414x896", @"scale": @"3x"},
        @{@"model": @"iPhone SE (2nd)", @"resolution": @"375x667", @"scale": @"2x"},
        @{@"model": @"iPhone 12 mini", @"resolution": @"375x812", @"scale": @"3x"},
        @{@"model": @"iPhone 12", @"resolution": @"390x844", @"scale": @"3x"},
        @{@"model": @"iPhone 12 Pro", @"resolution": @"390x844", @"scale": @"3x"},
        @{@"model": @"iPhone 12 Pro Max", @"resolution": @"428x926", @"scale": @"3x"},
        @{@"model": @"iPhone 13 mini", @"resolution": @"375x812", @"scale": @"3x"},
        @{@"model": @"iPhone 13", @"resolution": @"390x844", @"scale": @"3x"},
        @{@"model": @"iPhone 13 Pro", @"resolution": @"390x844", @"scale": @"3x"},
        @{@"model": @"iPhone 13 Pro Max", @"resolution": @"428x926", @"scale": @"3x"},
        @{@"model": @"iPhone SE (3rd)", @"resolution": @"375x667", @"scale": @"2x"},
        @{@"model": @"iPhone 14", @"resolution": @"390x844", @"scale": @"3x"},
        @{@"model": @"iPhone 14 Plus", @"resolution": @"428x926", @"scale": @"3x"},
        @{@"model": @"iPhone 14 Pro", @"resolution": @"393x852", @"scale": @"3x"},
        @{@"model": @"iPhone 14 Pro Max", @"resolution": @"430x932", @"scale": @"3x"},
        @{@"model": @"iPhone 15", @"resolution": @"393x852", @"scale": @"3x"},
        @{@"model": @"iPhone 15 Plus", @"resolution": @"430x932", @"scale": @"3x"},
        @{@"model": @"iPhone 15 Pro", @"resolution": @"393x852", @"scale": @"3x"},
        @{@"model": @"iPhone 15 Pro Max", @"resolution": @"430x932", @"scale": @"3x"}
    ];
    
    for (NSDictionary *device in iPhoneResolutions) {
        NSLog(@"%-20s: %@ (Scale: %@)",
              [device[@"model"] UTF8String],
              device[@"resolution"],
              device[@"scale"]);
    }
    
    NSLog(@"\n=== 逻辑分辨率分组 ===");
    NSLog(@"320x480: iPhone, iPhone 3G, iPhone 3GS, iPhone 4, iPhone 4S");
    NSLog(@"320x568: iPhone 5, iPhone 5c, iPhone 5s, iPhone SE (1st)");
    NSLog(@"375x667: iPhone 6, iPhone 6s, iPhone 7, iPhone 8, iPhone SE (2nd)");
    NSLog(@"414x736: iPhone 6 Plus, iPhone 6s Plus, iPhone 7 Plus, iPhone 8 Plus");
    NSLog(@"375x812: iPhone X, iPhone XS, iPhone 11 Pro, iPhone 12 mini, iPhone 13 mini");
    NSLog(@"414x896: iPhone XR, iPhone XS Max, iPhone 11, iPhone 11 Pro Max");
    NSLog(@"390x844: iPhone 12, iPhone 12 Pro, iPhone 13, iPhone 13 Pro");
    NSLog(@"428x926: iPhone 12 Pro Max, iPhone 13 Pro Max, iPhone 14 Plus");
    NSLog(@"393x852: iPhone 14 Pro, iPhone 15, iPhone 15 Pro");
    NSLog(@"430x932: iPhone 14 Pro Max, iPhone 15 Plus, iPhone 15 Pro Max");
}

+ (void)getCurrentDeviceLogicalResolution {
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGFloat scale = [[UIScreen mainScreen] scale];
    CGSize nativeSize = [[UIScreen mainScreen] nativeBounds].size;
    
    NSString *deviceModel = [self getDeviceModelName];
    
    NSLog(@"\n=== 当前设备信息 ===");
    NSLog(@"设备型号: %@", deviceModel);
    NSLog(@"逻辑分辨率: %.0fx%.0f", screenBounds.size.width, screenBounds.size.height);
    NSLog(@"物理分辨率: %.0fx%.0f", nativeSize.width, nativeSize.height);
    NSLog(@"缩放比例: %.1fx", scale);
    NSLog(@"屏幕尺寸 (points): %.1f x %.1f", screenBounds.size.width, screenBounds.size.height);
    NSLog(@"屏幕尺寸 (pixels): %.0f x %.0f",
          screenBounds.size.width * scale,
          screenBounds.size.height * scale);
    
    // 判断设备类型
    if (screenBounds.size.height >= 812) {
        NSLog(@"设备类型: 全面屏设备 (有刘海或灵动岛)");
    } else if (screenBounds.size.height >= 736) {
        NSLog(@"设备类型: Plus 系列大屏设备");
    } else if (screenBounds.size.height >= 667) {
        NSLog(@"设备类型: 标准大屏设备");
    } else if (screenBounds.size.height >= 568) {
        NSLog(@"设备类型: 4英寸设备");
    } else {
        NSLog(@"设备类型: 3.5英寸经典设备");
    }
}

+ (NSString *)getDeviceModelName {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    // 设备型号映射
    NSDictionary *deviceMapping = @{
        @"iPhone1,1": @"iPhone",
        @"iPhone1,2": @"iPhone 3G",
        @"iPhone2,1": @"iPhone 3GS",
        @"iPhone3,1": @"iPhone 4",
        @"iPhone3,2": @"iPhone 4",
        @"iPhone3,3": @"iPhone 4",
        @"iPhone4,1": @"iPhone 4S",
        @"iPhone5,1": @"iPhone 5",
        @"iPhone5,2": @"iPhone 5",
        @"iPhone5,3": @"iPhone 5c",
        @"iPhone5,4": @"iPhone 5c",
        @"iPhone6,1": @"iPhone 5s",
        @"iPhone6,2": @"iPhone 5s",
        @"iPhone7,1": @"iPhone 6 Plus",
        @"iPhone7,2": @"iPhone 6",
        @"iPhone8,1": @"iPhone 6s",
        @"iPhone8,2": @"iPhone 6s Plus",
        @"iPhone8,4": @"iPhone SE (1st)",
        @"iPhone9,1": @"iPhone 7",
        @"iPhone9,2": @"iPhone 7 Plus",
        @"iPhone9,3": @"iPhone 7",
        @"iPhone9,4": @"iPhone 7 Plus",
        @"iPhone10,1": @"iPhone 8",
        @"iPhone10,2": @"iPhone 8 Plus",
        @"iPhone10,3": @"iPhone X",
        @"iPhone10,4": @"iPhone 8",
        @"iPhone10,5": @"iPhone 8 Plus",
        @"iPhone10,6": @"iPhone X",
        @"iPhone11,2": @"iPhone XS",
        @"iPhone11,4": @"iPhone XS Max",
        @"iPhone11,6": @"iPhone XS Max",
        @"iPhone11,8": @"iPhone XR",
        @"iPhone12,1": @"iPhone 11",
        @"iPhone12,3": @"iPhone 11 Pro",
        @"iPhone12,5": @"iPhone 11 Pro Max",
        @"iPhone12,8": @"iPhone SE (2nd)",
        @"iPhone13,1": @"iPhone 12 mini",
        @"iPhone13,2": @"iPhone 12",
        @"iPhone13,3": @"iPhone 12 Pro",
        @"iPhone13,4": @"iPhone 12 Pro Max",
        @"iPhone14,2": @"iPhone 13 Pro",
        @"iPhone14,3": @"iPhone 13 Pro Max",
        @"iPhone14,4": @"iPhone 13 mini",
        @"iPhone14,5": @"iPhone 13",
        @"iPhone14,6": @"iPhone SE (3rd)",
        @"iPhone14,7": @"iPhone 14",
        @"iPhone14,8": @"iPhone 14 Plus",
        @"iPhone15,2": @"iPhone 14 Pro",
        @"iPhone15,3": @"iPhone 14 Pro Max",
        @"iPhone15,4": @"iPhone 15",
        @"iPhone15,5": @"iPhone 15 Plus",
        @"iPhone16,1": @"iPhone 15 Pro",
        @"iPhone16,2": @"iPhone 15 Pro Max"
    };
    
    NSString *mappedName = deviceMapping[deviceModel];
    return mappedName ? mappedName : deviceModel;
}

// 获取安全区域信息（iOS 11+）
+ (void)getSafeAreaInfo {
    if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        UIEdgeInsets safeAreaInsets = window.safeAreaInsets;
        
        NSLog(@"\n=== 安全区域信息 ===");
        NSLog(@"顶部安全区域: %.1f", safeAreaInsets.top);
        NSLog(@"底部安全区域: %.1f", safeAreaInsets.bottom);
        NSLog(@"左侧安全区域: %.1f", safeAreaInsets.left);
        NSLog(@"右侧安全区域: %.1f", safeAreaInsets.right);
        
        if (safeAreaInsets.top > 20) {
            NSLog(@"设备特征: 有刘海或灵动岛");
        }
        if (safeAreaInsets.bottom > 0) {
            NSLog(@"设备特征: 无Home键，有手势条");
        }
    }
}
+ (NSArray<NSDictionary *> *)alliPhoneLogicalResolutions {
    return @[
        // 传统机型 320
        @{@"name": @"iPhone 3GS", @"width": @320, @"height": @480, @"scale": @1.0},
        @{@"name": @"iPhone 4/4s", @"width": @320, @"height": @480, @"scale": @2.0},
        @{@"name": @"iPhone 5/5s/SE 1st", @"width": @320, @"height": @568, @"scale": @2.0},
        
        // 4.7英寸系列 375
        @{@"name": @"iPhone 6/7/8", @"width": @375, @"height": @667, @"scale": @2.0},
        @{@"name": @"iPhone SE 2nd/3rd", @"width": @375, @"height": @667, @"scale": @2.0},
        
        // Plus机型
        @{@"name": @"iPhone 6/7/8 Plus", @"width": @414, @"height": @736, @"scale": @3.0},
        
        // 全面屏系列
        // 5.8英寸  375
        @{@"name": @"iPhone X/XS/11 Pro", @"width": @375, @"height": @812, @"scale": @3.0},
        // 6.1英寸
        @{@"name": @"iPhone XR/11", @"width": @414, @"height": @896, @"scale": @2.0},
        
        @{@"name": @"iPhone 12/13/14/15", @"width": @390, @"height": @844, @"scale": @3.0},
        @{@"name": @"iPhone 12/13 Pro", @"width": @390, @"height": @844, @"scale": @3.0},
        // 6.5英寸
        @{@"name": @"iPhone XS Max/11 Pro Max", @"width": @414, @"height": @896, @"scale": @3.0},
        // 6.7英寸
        @{@"name": @"iPhone 12/13/14 Pro Max", @"width": @428, @"height": @926, @"scale": @3.0},
        
        @{@"name": @"iPhone 15 Pro Max", @"width": @430, @"height": @932, @"scale": @3.0},
        
        // mini系列
        @{@"name": @"iPhone 12/13 mini", @"width": @360, @"height": @780, @"scale": @3.0},
        
        // iPad兼容模式
        @{@"name": @"iPad兼容模式", @"width": @375, @"height": @512, @"scale": @2.0}
    ];
}

@end
